/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Painting.java
 *
 * Created on 2008-11-11, 21:09:15
 */
package upgmdendro.drawing;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import upgmadendro.diagram.DiagramStructure;
import upgmdendro.drawing.drawables.Drawable;

/**
 *
 * @author psychollek
 */
public class Painting extends javax.swing.JFrame {

    private DiagramStructure ds = null;

    /** Creates new form Painting */
    public Painting(DiagramStructure ds) {
        this.ds = ds;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new Canvas(ds);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 410, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 296, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}

class Canvas extends javax.swing.JPanel {
    
    private DiagramStructure ds = null;
    
    Canvas(DiagramStructure ds){
        this.ds = ds;
    }
    
    private PictureResolver pr = new PictureResolver();

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        GraphicParameters.setGraphics(g);

        if (GraphicParameters.getHeight() != this.getHeight() 
                || GraphicParameters.getWidth() != this.getWidth()) {
            GraphicParameters.setHeight(this.getHeight());
            GraphicParameters.setWidth(this.getWidth());
            pr.resolveStructure(ds);
        }
        
        ArrayList<Drawable> dr = pr.getPicture();
        
        for (Iterator<Drawable> it = dr.iterator(); it.hasNext();) {
            Drawable drawable = it.next();
            drawable.paint();
        }

    }
}
